package com.dreamoo.aicommon.websearch.conf;

import dev.langchain4j.community.web.search.searxng.SearXNGWebSearchEngine;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 山顶洞人郑某
 * @className SearXngConf.java SearXng搜索引擎配置
 * @date 2025 04 30
 */
@Configuration
public class SearXngConf {

    @Bean
    public SearXNGWebSearchEngine searXNGWebSearchEngine() {
        Map<String ,Object> params = new HashMap<>();
        params.put("categories","general");
        // 从disabled_engines中移除bing相关引擎
        params.put("disabled_engines",
                "arch linux wiki__it,artic__images,arxiv__science,bandcamp__music,wikipedia__general,openverse__images,chefkoch__other,currency__general,deviantart__images,docker hub__it,wikidata__general,duckduckgo__general,etymonline__other,flickr__images,genius__music,gentoo__it,github__it,google__general,google images__images,google news__news,google videos__videos,google scholar__science,hoogle__it,kickass__files,lemmy communities__social media,lemmy users__social media,lemmy posts__social media,lemmy comments__social media,z-library__files,library of congress__images,lingva__general,mastodon users__social media,mastodon hashtags__social media,mdn__it,mixcloud__music,mankier__it,openairedatasets__science,openairepublications__science,openstreetmap__map,pdbe__science,photon__map,pinterest__images,piped__videos,piped.music__music,piratebay__files,podcastindex__other,public domain image archive__images,pubmed__science,pypi__it,qwant__general,qwant news__news,qwant images__images,qwant videos__videos,radio browser__music,sepiasearch__videos,soundcloud__music,stackoverflow__it,askubuntu__it,superuser__it,startpage__general,startpage news__news,startpage images__images,solidtorrents__files,unsplash__images,yahoo news__news,youtube__videos,youtube__music,dailymotion__videos,vimeo__videos,wikinews__news,wiktionary__other,wikicommons.images__images,wikicommons.videos__videos,wikicommons.audio__music,wikicommons.files__files,dictzone__general,mymemory translated__general,wordnik__other,tootfinder__social media,wallhaven__images,wttr.in__other,brave__general,brave.images__images,brave.videos__videos,brave.news__news,bt4g__files");
        // 在enabled_engines中添加bing相关引擎
        params.put("enabled_engines","360search__general,360search videos__videos,baidu__general,baidu images__images,baidu kaifa__it,chinaso news__other,chinaso images__other,chinaso videos__other,iqiyi__videos,sogou__general,sogou videos__videos,sogou wechat__news,bing__general,bing images__images,bing news__news,bing videos__videos");
        // 添加max_redirects参数解决重定向问题
        params.put("max_redirects", 10);
        // 设置bing搜索引擎的base_url为中国版
        params.put("bing__general.base_url", "https://cn.bing.com/search");

        return SearXNGWebSearchEngine.builder()
                .baseUrl("http://localhost:8180/")
                .logResponses(true)
                .logRequests(true)
                .optionalParams(params)
                .duration(Duration.ofSeconds(10))
                .build();
    }

}
